package ar.com.utn.openqsb.lip.ui;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;

import ar.com.utn.openqsb.lip.core.Constraint;
import ar.com.utn.openqsb.lip.core.Exercise;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class ExerciseDataPanel extends JPanel
{

    private static final long serialVersionUID = 1L;

    private List<JFormattedTextField> objectiveCoefficients;

    private JFormattedTextField constantTerm;

    private List<List<JFormattedTextField>> constraintCoefficients;

    private List<JComboBox> relations;

    private List<JFormattedTextField> rightValues;

    public ExerciseDataPanel(final Exercise descriptor)
    {
        int x;
        int y;

        // design form
        FormLayout layout = buildLayout(descriptor);
        PanelBuilder builder = new PanelBuilder(layout);
        CellConstraints cc = new CellConstraints();
        JFormattedTextField valueField;

        // add objective function fields
        objectiveCoefficients = new ArrayList<JFormattedTextField>();
        builder.addLabel("Z", cc.xy(1, 2));
        for (int i = 0; i < descriptor.getNumVars(); i++)
        {
            // determine position
            x = i + 2;
            y = 2;
            // add column label
            builder.addLabel("X" + i, cc.xy(i + 2, 1));
            // create component
            valueField = new JFormattedTextField(DecimalFormat.getInstance());
            valueField.setValue(descriptor.getModel().getObjective().getCoefficients().getData()[i]);
            objectiveCoefficients.add(valueField);
            // add component to form
            builder.add(objectiveCoefficients.get(i), cc.xy(x, y));
        }
        // add objective function constant term
        x = descriptor.getNumVars() + 2;
        y = 2;
        constantTerm = new JFormattedTextField(DecimalFormat.getInstance());
        constantTerm.setValue(descriptor.getModel().getObjective().getConstantTerm());
        builder.add(constantTerm, cc.xy(x, y));

        // add constraint coefficient fields
        constraintCoefficients = new ArrayList<List<JFormattedTextField>>();
        for (int i = 0; i < descriptor.getNumConstraints(); i++)
        {
            // add row label
            builder.addLabel("C" + i, cc.xy(1, i + 3));
            constraintCoefficients.add(new ArrayList<JFormattedTextField>());
            for (int j = 0; j < descriptor.getNumVars(); j++)
            {
                // determine position
                x = j + 2;
                y = i + 3;
                // create component
                valueField = new JFormattedTextField(DecimalFormat.getInstance());
                valueField.setValue(descriptor.getModel().getConstraints().get(i).getCoefficients().getData()[j]);
                constraintCoefficients.get(i).add(valueField);
                // add component to form
                builder.add(constraintCoefficients.get(i).get(j), cc.xy(x, y));
            }
        }

        // add relations
        relations = new ArrayList<JComboBox>();
        for (int i = 0; i < descriptor.getNumConstraints(); i++)
        {
            // determine position
            x = descriptor.getNumVars() + 2;
            y = i + 3;
            // create component
            relations.add(new JComboBox(new Constraint.Relation[]
            { Constraint.Relation.GE, Constraint.Relation.EQ, Constraint.Relation.LE }));
            relations.get(i).setSelectedItem(descriptor.getModel().getConstraints().get(i).getRelation());
            // add component to form
            builder.add(relations.get(i), cc.xy(x, y));

        }

        // add right side
        rightValues = new ArrayList<JFormattedTextField>();
        for (int i = 0; i < descriptor.getNumConstraints(); i++)
        {
            // determine position
            x = descriptor.getNumVars() + 3;
            y = i + 3;
            // create component
            rightValues.add(new JFormattedTextField(DecimalFormat.getInstance()));
            rightValues.get(i).setValue(descriptor.getModel().getConstraints().get(i).getRightValue());
            // add component to form
            builder.add(rightValues.get(i), cc.xy(x, y));
        }

        // build the form
        add(builder.getPanel());
    }

    public FormLayout buildLayout(final Exercise descriptor)
    {
        StringBuilder columns = new StringBuilder();
        int c = descriptor.getNumVars();
        while (c-- > 0)
        {
            columns.append("40dlu, ");
        }
        columns.append("40dlu, ");
        columns.append("30dlu, ");
        columns.append("40dlu");
        StringBuilder rows = new StringBuilder();
        int r = descriptor.getNumConstraints();
        while (r-- > 0)
        {
            rows.append("20dlu, ");
        }
        rows.append("20dlu, ");
        rows.append("20dlu");
        return new FormLayout(columns.toString(), rows.toString());
    }

    /**
     * @return the objectiveCoefficients
     */
    public List<JFormattedTextField> getObjectiveCoefficients()
    {
        return objectiveCoefficients;
    }

    /**
     * 
     * @return the objective constant term. field
     */
    public JFormattedTextField getConstantTerm()
    {
        return constantTerm;
    }

    /**
     * @return the constraintCoefficients
     */
    public List<List<JFormattedTextField>> getConstraintCoefficients()
    {
        return constraintCoefficients;
    }

    /**
     * @return the relations
     */
    public List<JComboBox> getRelations()
    {
        return relations;
    }

    /**
     * @return the rightValues
     */
    public List<JFormattedTextField> getRightValues()
    {
        return rightValues;
    }

}
